package com.fit.dao.company;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.entities.Company;
import com.fit.entities.LicencePlate;
import com.fit.entities.User;

public class CompanyDAOImpl implements CompanyDAO {

	@Inject
	private Session session;

	public boolean save(Company company) {
		if (company != null) {
			session.save(company);
			return true;
		}
		return false;
	}

	public Company findById(Long id) {
		return (Company) session.get(Company.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Company> findAll() {
		return session.createCriteria(Company.class).list();
	}

	public boolean deleteById(Long id) {
		Company c = findById(id);
		if (c != null) {
			session.delete(c);
			return true;
		}
		return false;
	}

	public boolean update(Company company) {
		if (company != null) {
			session.update(company);
			return true;
		}
		return false;
	}

	public boolean checkUsers(Company company) {
		ArrayList<User> list = (ArrayList<User>) session.createCriteria(User.class).add(Restrictions.eq("company", company)).list();
		if(list.size()!=0){
			return true;
		}
		return false;
	}
	
	public boolean checkRegistrations(Company company) {
		ArrayList<LicencePlate> list = (ArrayList<LicencePlate>) session.createCriteria(LicencePlate.class).add(Restrictions.eq("company", company)).list();
		if(list.size()!=0){
			return true;
		}
		return false;
	}
}
